#!/bin/bash

echo "DmSQL 打包脚本"
echo "==============================="

# 检查是否安装了Ant
if ! command -v ant &> /dev/null; then
    echo "错误: 未找到Apache Ant。请安装Apache Ant并确保其在PATH环境变量中。"
    echo "您可以使用系统包管理器安装Apache Ant，例如："
    echo "  Debian/Ubuntu: sudo apt-get install ant"
    echo "  CentOS/RHEL: sudo yum install ant"
    echo "  macOS: brew install ant"
    exit 1
fi

# 检查参数
if [ -z "$1" ]; then
    echo "用法: ./build.sh [clean|jar|run]"
    echo "  clean - 清理构建文件"
    echo "  jar   - 构建可执行JAR文件"
    echo "  run   - 构建并运行应用程序"
    exit 0
fi

# 确保脚本可执行
chmod +x build.sh

# 执行Ant任务
case "$1" in
    clean)
        echo "清理构建文件..."
        ant clean
        ;;
    jar)
        echo "构建可执行JAR文件..."
        ant jar
        if [ $? -eq 0 ]; then
            echo ""
            echo "构建成功！JAR文件位于 dist/DmSQL.jar"
            echo "运行命令: java -jar dist/DmSQL.jar -c run.properties -a"
            echo "或者: java -jar dist/DmSQL.jar -c run.properties -r -g"
        fi
        ;;
    run)
        echo "构建并运行应用程序..."
        ant run
        ;;
    *)
        echo "未知参数: $1"
        echo "用法: ./build.sh [clean|jar|run]"
        exit 1
        ;;
esac

exit 0